//
//  JZNearbyViewController.m
//  QuanZiChat
//
//  Created by 张洪林 on 15/11/30.
//  Copyright © 2015年 ZhongHongLin. All rights reserved.
//

#import "JZNearbyViewController.h"
#import "GetNearedPresonManager.h"
#import "NearbyContactCell.h"
#import "MJRefresh.h"
@interface JZNearbyViewController ()<CLLocationManagerDelegate,UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *listtebleView;
@property(nonatomic,strong)NSMutableArray *nearArray;
@property(nonatomic,strong)NSMutableArray *sourceArray;
@property(nonatomic,assign)int page;
@end

@implementation JZNearbyViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    [self.listtebleView reloadData];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 0;
    
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager startUpdatingLocation];
    [self loadData];
    [self.listtebleView reloadData];
    [self.listtebleView addHeaderWithTarget:self action:@selector(refresh)];
    [self.listtebleView addFooterWithTarget:self action:@selector(loadMoreData)];
    
    // Do any additional setup after loading the view.
}


-(void)loadData{
    
    [[GetNearedPresonManager manager] getNearedPreson:^(NSMutableArray *result) {
        [self.nearArray removeAllObjects];
        [self.sourceArray removeAllObjects];
        [self.nearArray addObjectsFromArray:result];
    }];
    for (int i = 10 * self.page ; i < self.nearArray.count; i++) {
        if (i == 10 * (self.page + 1)) {
            return;
        }
        [self.sourceArray addObject:self.nearArray[i]];
    }
    
}

-(void)loadMoreData{
    self.page += 1;
    if (self.page * 10 > self.nearArray.count) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有更多数据了" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
            return;
        }];
        
        [alert addAction:alertA];
        [self showViewController:alert sender:nil];
    }
    for (int i = 10 * self.page ; i < self.nearArray.count; i++) {
        if (i == 10 * (self.page + 1)) {
            return;
        }
        [self.sourceArray addObject:self.nearArray[i]];
    }
    [self.listtebleView footerEndRefreshing];
    [self.listtebleView reloadData];
}
-(void)refresh{
    self.page = 0;
    [self.locationManager startUpdatingLocation];
    [self loadData];
    [self.listtebleView headerEndRefreshing];
    [self.listtebleView reloadData];

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.sourceArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.presonInfo = self.sourceArray[indexPath.row];
    NearbyContactCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NearbyContactCell" forIndexPath:indexPath];
    
    cell.presonInfo = self.presonInfo;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 77;
}



-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    self.checkinLocation = [locations lastObject];
    CLLocationCoordinate2D cool = self.checkinLocation.coordinate;
    self.currentLatitude  = [NSString stringWithFormat:@"%.4f",cool.latitude];
    self.currentLongitude = [NSString stringWithFormat:@"%.4f",cool.longitude];
    
    AVGeoPoint *point = [AVGeoPoint geoPointWithLatitude:cool.latitude longitude:cool.longitude];
    AVUser *currentUser = [AVUser currentUser];
    [currentUser setObject:point forKey:@"location"];
    NSLog(@"%@,%@",point,currentUser);
    [currentUser save];
    [manager stopUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    if (error.code == kCLErrorDenied) {
        NSLog(@"-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-%@",error);
    }
}

-(NSMutableArray *)nearArray{
    if (_nearArray == nil) {
        _nearArray = [NSMutableArray new];
    }
    return _nearArray;
}
-(NSMutableArray *)sourceArray{
    if (_sourceArray == nil) {
        _sourceArray = [NSMutableArray new];
    }
    return _sourceArray;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
